package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.util.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    //查询所有员工并分页展示
    @GetMapping
    public Result pageQuery(String name, Integer gender,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize)
    {
        log.info("分页查询员工列表，page={}, pageSize={}, name={}, gender={}, " +
                        "begin={}, end={}",
                page, pageSize, name, gender, begin, end);

        PageBean<Emp> pageBean = empService.pageQuery(name,gender,begin,end,page,pageSize);
        return Result.success(pageBean);
    }

    //新增员工
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp) throws Exception {
        log.info("新增员工，emp={}", emp);
        empService.add(emp);

        return Result.success();
    }

    //删除员工
    @Log
    @DeleteMapping
    public Result Delete(@RequestParam("ids") List<Integer> ids){
        log.info("删除员工，id={}",ids);
        empService.deleteById(ids);
        return Result.success();
    }

    //修改员工-查询回显 GET /emps/{id}
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){

        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    //修改员工 PUT /emps
    @Log
    @PutMapping
    public Result putEmp(@RequestBody Emp emp){
        empService.putEmp(emp);
        return Result.success();
    }

    //查询全部员工
    @GetMapping("/list")
    public Result GetAllEmp(){
        List<Emp> lists = empService.getAllEmp();
        return Result.success(lists);
    }

}